//strlen    strcpy strnpy   strcat strncat   strcmp strncmp
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

size_t my_strlen(const char *s)
{
    size_t len = 0;
    if(s == NULL)
    {
        return 0;
    }
    while(*s != '\0')
    {
        len++;
        s++;
    }
    return len;
}

char * my_strcpy(char *dest,char *src)
{
    if(dest == NULL || src == NULL)
    {
        return NULL;
    }
    char *temp = dest;//通过新的字符串 “替死鬼”

    while(*src != '\0')
    {
        *temp = *src;
        temp++;
        src++;
    }
    *temp = '\0';
    return dest;
}

char * my_strncpy(char *dest,char *src,size_t n)
{
    if(dest == NULL || src == NULL)
    {
        return NULL;
    }
    char *temp = dest;
    int i;
    for(i = 0;(i < n) && (*src != '\0') ; i++)
    {
        *temp = *src;
        temp++;
        src++;
    }
    for(; i < n; i++)//确定拷贝次数
    {
        *temp = '\0';
        temp++;
    }
    //*temp = '\0';
    return dest;
}

char * my_strcat(char *dest,char *src)
{
    if(dest == NULL || src == NULL)
    {
        return NULL;
    }

    char *temp = dest;

    //指向temp的末尾'\0'
    while(*temp != '\0')
    {
        temp++;
    }

    while(*src != '\0')
    {
        *temp = *src;
        temp++;
        src++;
    }
    temp = '\0';
    return dest;
}

char * my_strncat(char *dest,char *src,size_t n)
{
    if(dest == NULL || src == NULL)
    {
        return NULL;
    }

    char *temp = dest;

    while(*temp != '\0')
    {
        temp++;
    }

    for(int i = 0;(i < n) && (*src != '\0') ; i++)
    {
        *temp = *src;
        temp++;
        src++;
    }

    *temp = '\0';
    return dest;
} 

int my_strcmp(char *s1,char *s2)
{
    if(s1 == NULL && s2 != NULL)
    {
        return -1;
    }

    if(s2 == NULL && s1 != NULL)
    {
        return 1;
    }

    if(s1 == NULL && s2 != NULL)
    {
        return 0;
    }

    while(*s1 != '\0' && *s2 != '\0')
    {
        if(*s1 > *s2)
        {
            return 1;
        }
        if(*s1 < *s2)
        {
            return -1;
        }
        s1++;
        s2++;
    }

    if(*s1 == '\0' && *s2 == '\0')
    {
        return 0;
    }
    if(*s1 != '\0' && *s2 == '\0')
    {
        return 1;
    }
    if(*s1 == '\0' && *s2 != '\0')
    {
        return -1;
    }
}

int my_strncmp(char *s1,char *s2,size_t n)
{
    if(s1 == NULL || s2 == NULL)
    {
        return -1;
    }

    for(int i = 0;(i < n) && ((s1[i] != '\0') && (s2[i] != '\0')) ; i++)
    {
        if(s1[i] != s2[i])
        {
            return -1;
        }
    }

    return 0;
}

int main(int argc,char *argv[])
{
    char *ptr = "hello world";

    char src[100] = "hello world";

    char *dest = (char *)malloc(sizeof(char) * 100);
    if(dest == NULL)
    {
        printf("malloc error!\n");
        exit(1);
    }
    
    //strcpy(dest,"hello");

    //size_t是无符号长整形unsigned long
    //size_t len = strlen(src);
    size_t len = my_strlen(src);
    size_t len1 = strlen("hello world");

    printf("len = %ld\n",len);
    //printf("len1 = %ld\n",strlen(src));

    //char *temp = strcpy(dest,src);    //strcpy_s(char *dest,int len,char *src)
    //char *temp = my_strcpy(dest,src);

    //char *temp = strncpy(dest,src,100);
    //char *temp = my_strncpy(dest,src,100);

    //char *temp = strcat(dest,src);
    //char *temp = my_strcat(dest,src);

    //char *temp = strncat(dest,src,5);
    //char *temp = my_strncat(dest,src,5);

    // if(temp == NULL)
    // {
    //     printf("my strpy is error!\n");
    //     exit(1);
    // }

    // printf("dest = %s\n",dest);
    // printf("temp = %s\n",temp);

    // printf("dest = %p\n",dest);
    // printf("temp = %p\n",temp);

    strcpy(dest,"hello world");
    //int ret = strcmp(dest,src);
    //int ret = my_strcmp(dest,src);

    int ret = my_strncmp(dest,src,my_strlen(dest));
    printf("ret = %d\n",ret);
    return 0;
}